package com.liliudong.rommath.biz.sys.service.impl;


import org.springframework.stereotype.Service;
import com.liliudong.rommath.biz.sys.service.SysUserRoleService;
import com.liliudong.rommath.biz.sys.entity.SysUserRole;
import com.liliudong.rommath.biz.sys.mapper.SysUserRoleMapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;

import java.util.List;

import static com.liliudong.rommath.biz.sys.entity.table.SysUserRoleTableDef.SYS_USER_ROLE;

/**
 * 用户角色关联表 服务层实现。
 *
 * @author liudong.li
 * @since 1.0.0
 */
@Service
public class SysUserRoleServiceImpl extends ServiceImpl<SysUserRoleMapper, SysUserRole> implements SysUserRoleService {

    @Override
    public boolean existsByRoleId(Long roleId) {
        return queryChain()
                .where(SYS_USER_ROLE.ROLE_ID.eq(roleId))
                .exists();
    }

    @Override
    public void saveByUserId(Long sysUserId, Long roleId) {
        SysUserRole sysUserRole = new SysUserRole();
        sysUserRole.setUserId(sysUserId);
        sysUserRole.setRoleId(roleId);
        save(sysUserRole);
    }

    @Override
    public void saveBatchByUserId(Long sysUserId, List<Long> roleIdList) {
        List<SysUserRole> sysUserRoles = roleIdList.stream()
                .map(roleId -> {
                    SysUserRole sysUserRole = new SysUserRole();
                    sysUserRole.setUserId(sysUserId);
                    sysUserRole.setRoleId(roleId);
                    return sysUserRole;
                })
                .toList();
        saveBatch(sysUserRoles);
    }
}